//
// Coef.as - polynomial coefficients for parametric polynomials of arbitrary order.    
//
// copyright (c) 2006-2007, Jim Armstrong.  All Rights Reserved.
//
// This software program is supplied 'as is' without any warranty, express, implied, 
// or otherwise, including without limitation all warranties of merchantability or fitness
// for a particular purpose.  Jim Armstrong shall not be liable for any special incidental, 
// or consequential damages, including, without limitation, lost revenues, lost profits, 
// or loss of prospective economic advantage, resulting from the use or misuse of this 
// software program.
//
// Programmed by Jim Armstrong, Singularity (www.algorithmist.net)
//
// Note:  This is a crude implementation, meant to support the aribitrary-order Bezier curve which is itself
// used only for teaching purposes.
//
//

package singularity.geom;

  class Coef
   {
    // properties
    
    public var degree(getDegree, null) : Int ;
    // properties
    var __cX:Array<Dynamic>;
    var __cY:Array<Dynamic>;
    var __len:Int;
    
    public function new()
    {
      __cX  = new Array();
      __cY  = new Array();
      __len = 0;
    }
    
    public function getDegree():Int { return __cX.length; }

    public function reset():Void
    {
      __cX = [];
      __cY = [];
      __len = 0;
    }
    
    public function addCoef( _cX:Float, _cY:Float ):Void
    {
      __cX.push(_cX);
      __cY.push(_cY);
      __len++;
    }
    
    public function getCoef( _indx:Int ):Dynamic 
    { 
      return {X:__cX[_indx], Y:__cY[_indx]}
    }

    public function getX(_t:Float):Float
    {
      if( __len > 1 )
      {
        var p:Float = __cX[__len - 1];
		var i:Int=__len-2;
        while (  i >= 0) {
			p = _t * p + __cX[i];
			i-- ;
		}
               
        return p;
      }
      else
        return __cX[0];
    }

    public function getY(_t:Float):Float
    {
      if( __len > 1 )
      {
        var p:Float = __cY[__len - 1];
		var i:Int=__len-2;
        while(  i>=0){
			p = _t * p + __cY[i];
			i--;
		}
        
        return p;
      }
      else
        return __cY[0];
    }
  }
